﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


public partial class reports_report_salary : System.Web.UI.Page
{

    ReportDocument rptDoc = new ReportDocument();
    ParameterField paramField = new ParameterField();
    ParameterFields paramFields = new ParameterFields();
    ParameterDiscreteValue paramDiscretevalues = new ParameterDiscreteValue();
    ParameterValues paramValues = new ParameterValues();
    DataSet ds;
    long sum = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] == null && Session["fullname"] == null)
                Response.Redirect("../login/login.aspx");
            else
                lUsername.Text = " " + Session["username"].ToString();
         
        }
    }
    protected void Chart_Info_USD()
    {
        
        chartSalaryUSD.Series["Series1"].Points.AddXY("Minimun Salary", Convert.ToDouble(ds.Tables[0].Rows[0]["min_usd"].ToString()));
        chartSalaryUSD.Series["Series1"].Points.AddXY("Num records", Convert.ToDouble(ds.Tables[0].Rows[0]["n3"].ToString()));
        chartSalaryUSD.Series["Series1"].Points.AddXY("Maximun Salary", Convert.ToDouble(ds.Tables[0].Rows[0]["max_usd"].ToString()));
        chartSalaryUSD.Series["Series1"].Points.AddXY("Num records", Convert.ToInt32(ds.Tables[0].Rows.Count));

    }
    protected void Chart_Info_VND()
    {

        chartSalaryVND.Series["Series1"].Points.AddXY("Minimun Salary", Convert.ToDouble(ds.Tables[0].Rows[0]["min_vnd"].ToString()));
        chartSalaryVND.Series["Series1"].Points.AddXY("Num records", Convert.ToDouble(ds.Tables[0].Rows[0]["n1"].ToString()));
        chartSalaryVND.Series["Series1"].Points.AddXY("Maximun Salary", Convert.ToDouble(ds.Tables[0].Rows[0]["max_vnd"].ToString()));
        chartSalaryVND.Series["Series1"].Points.AddXY("Num records", Convert.ToDouble(ds.Tables[0].Rows[0]["n2"].ToString()));        
        
    }
    protected void Info_Salary()
    {
        string s1=string.Empty, s2=string.Empty;
        Report rpt = new Report();
        

        if (txtFrom.Text != ""&&txtTo.Text!="")
        {
            s1 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFrom.Text);
            s2 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtTo.Text);
            rpt.From = DateTime.Parse(s1);
            rpt.To = DateTime.Parse(s2);
            ds = rpt.Info_Salary();


            long number1=0,number2=0,number3=0,number4=0;
            if (ds.Tables[0].Rows[0]["min_vnd"].ToString() != "")
            {
                number1 = Convert.ToInt32(ds.Tables[0].Rows[0]["min_vnd"].ToString());
                ltMinVND.Text = String.Format("{0:c0}", number1);
            }
            else ltMinVND.Text = "0";

            ltNumMinVND.Text = ds.Tables[0].Rows[0]["n1"].ToString();

            if (ds.Tables[0].Rows[0]["max_vnd"].ToString() != "")
            {
                number2 = Convert.ToInt32(ds.Tables[0].Rows[0]["max_vnd"].ToString());
                ltMaxVND.Text = String.Format("{0:c0}", number2);
            }
            else ltMaxVND.Text = "0";

            ltNumMaxVND.Text = ds.Tables[0].Rows[0]["n2"].ToString();

            if (ds.Tables[0].Rows[0]["min_usd"].ToString() != "")
            {
                number3 = Convert.ToInt32(ds.Tables[0].Rows[0]["min_usd"].ToString());
                ltMinUSD.Text = String.Format("{0:c0}", number3);
            }
            else ltMinUSD.Text = "0";

            ltNumMinUSD.Text = ds.Tables[0].Rows[0]["n3"].ToString();

            if (ds.Tables[0].Rows[0]["max_usd"].ToString() != "")
            {
                number4 = Convert.ToInt32(ds.Tables[0].Rows[0]["max_usd"].ToString());
                ltMaxUSD.Text = String.Format("{0:c0}", number4);
            }
            else ltMaxUSD.Text = "0";

            ltNumMaxUSD.Text = ds.Tables[0].Rows[0]["n4"].ToString();

            if (ds.Tables[0].Rows[0]["n1"].ToString() == "")
                 number1 = 0;
            if (ds.Tables[0].Rows[0]["n2"].ToString() == "")
                number2 = 0;
            if (ds.Tables[0].Rows[0]["n3"].ToString() == "")
                number3 = 0;
            if (ds.Tables[0].Rows[0]["n4"].ToString() == "")
                number4 = 0;

            if (ds.Tables[0].Rows[0]["n4"].ToString() != ds.Tables[0].Rows[0]["n3"].ToString()
                && ds.Tables[0].Rows[0]["n2"].ToString() != ds.Tables[0].Rows[0]["n1"].ToString()
                )
                
            { 
                number1=Convert.ToInt32(ds.Tables[0].Rows[0]["n1"].ToString());
                number2=Convert.ToInt32(ds.Tables[0].Rows[0]["n2"].ToString());
                number3=Convert.ToInt32(ds.Tables[0].Rows[0]["n3"].ToString());
                number4=Convert.ToInt32(ds.Tables[0].Rows[0]["n4"].ToString());

                sum=number1+number2+number3+number4;
                ltTotalItems.Text = sum.ToString();
            }

            else
                if (ds.Tables[0].Rows[0]["n4"].ToString() == ds.Tables[0].Rows[0]["n3"].ToString()
                && ds.Tables[0].Rows[0]["n2"].ToString() == ds.Tables[0].Rows[0]["n1"].ToString()
                )
                {
                    number1 = Convert.ToInt32(ds.Tables[0].Rows[0]["n1"].ToString());                    
                    number3 = Convert.ToInt32(ds.Tables[0].Rows[0]["n3"].ToString());
                    

                    sum = number1 + number3;
                    ltTotalItems.Text = sum.ToString()+"/ "+ds.Tables[0].Rows.Count.ToString();

                }
        }
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        Info_Salary();
        Chart_Info_VND();
        Chart_Info_USD();
    }

    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        //FormsAuthentication.SignOut();

        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {

        /*
        paramField.Name = "@from";
        string s1 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFrom.Text);
        paramDiscretevalues.Value = s1;
        paramField.CurrentValues.Add(paramDiscretevalues);
        paramFields.Add(paramField);


        paramField = new ParameterField();
        paramField.Name = "@to";
        string s2 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtTo.Text);
        paramDiscretevalues = new ParameterDiscreteValue();
        paramDiscretevalues.Value = s2;
        paramField.CurrentValues.Add(paramDiscretevalues);
        paramFields.Add(paramField);
        rptView.ParameterFieldInfo = paramFields;

        rptDoc.Load(Server.MapPath("~/reports/rpt_knownfrom2.rpt"));
        Common.SetConnectionInfo(rptDoc.Database.Tables);
        //rptView.ReportSource = rptDoc;

        //rptView.Visible = true;
        rptDoc.SetParameterValue("@from", s1);
        rptDoc.SetParameterValue("@to", s2);

        rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("~/export/knownfrom2.pdf"));
        ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('../export/knownfrom2.pdf');popup.focus();", true);
        */
    }
}
